package com.gustuy.learn1;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        Socket client=null;
        InputStream is=null;
        ByteArrayOutputStream baos=null;
        {
            try {
                serverSocket = new ServerSocket(50010);
                client=serverSocket.accept();
                is =client.getInputStream();
                //管道流
                byte[] buffer=new byte[1024];
                baos=new ByteArrayOutputStream();
                int len;
                while((len=is.read(buffer))!=-1){
                    baos.write(buffer,0,len);
                }
                System.out.println(baos.toString());

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(baos!=null)
                        baos.close();
                    if(is!=null)
                        is.close();
                    if(client!=null)
                        client.close();
                    if(serverSocket!=null)
                        serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
